//
//  ZXLHomePageViewController.m
//  EPA
//
//  Created by 朱兴隆 on 16/4/15.
//  Copyright © 2016年 朱兴隆. All rights reserved.
//

#import "ZXLHomePageViewController.h"
#import "CK_MenuViewController.h"
#import "ZXLNetWorkRequestManager.h"
#import "ZXLCollectionViewCell.h"

#import "ZXLCycleView.h"

#import "ZXLHomeDetailController.h"

#import "ZXLRadioController.h"

#import "ZXLGoodsController.h"

#import "ZXLTopicController.h"
@interface ZXLHomePageViewController ()<MenuClickDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
// CollectionView
@property (nonatomic, strong) UICollectionView *collectionView;
// 数组属性
@property (nonatomic, strong) NSMutableArray *dataSource;
// 轮播图属性
@property (nonatomic, strong) ZXLCycleView *cycleView;
@end

@implementation ZXLHomePageViewController

#pragma mark - MenuClickDelegate

- (void)viewDidLoad {
    [super viewDidLoad];
    [CK_MenuViewController getMenuViewController].menuDelegate = self;
    [CK_MenuViewController getMenuViewController].menuArray = [@[@"今日 · TODAY",@"话题 · TOPIC",@"音乐 · MUSIC",@"良品 · PRODUCTS",@"订阅 · SUBSCRITION",@"设置 · SETTING"] mutableCopy];

    self.dataSource = [NSMutableArray new];
    [ZXLNetWorkRequestManager requestURL:[NSURL URLWithString:READLIST_URL] Method:ZXL_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDict = jsonDict[@"data"];
            for (NSDictionary *itemDict in dataDict[@"list"]) {
                ZXLHomeModel *model = [ZXLHomeModel new];
                [model setValuesForKeysWithDictionary:itemDict];
                [self.dataSource addObject:model];
            }
        }
        // 返回主线程刷新
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
            self.cycleView.imgNameArray = [@[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg"] mutableCopy];
        });
        
    }];
    
    // 创建collectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];

    // 设置layout
    layout.itemSize = CGSizeMake(self.view.frame.size.width * 0.3, self.view.frame.size.height * 0.25);
    // 边距
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 200);
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    // 注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZXLCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    // 注册头
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    self.cycleView = [[ZXLCycleView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) interVal:3];
    [self.cycleView addTapBlock:^(NSInteger index) {
        NSLog(@"点击第%ld页",index);
    }];
}
#pragma mark - UICollectionViewDelegate
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        // 创建头轮播图
        
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        [header addSubview:self.cycleView];
        return header;
        
    }
    return nil;
}
// 点击跳转
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZXLHomeDetailController *detailVC = [[ZXLHomeDetailController alloc] init];
    
//    ZXLHomeModel的type -> ZXLHomeDetailVC的type 
    ZXLHomeModel *model = self.dataSource[indexPath.row];
    detailVC.type = [NSString stringWithFormat:@"%ld",model.type];
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - UICollectionDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZXLCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    
    return cell;
}

// 点击抽屉按钮
- (IBAction)didClickLeftBarBtnItem:(UIBarButtonItem *)sender {
    // 判断菜单展示状态
    if ([CK_MenuViewController getMenuViewController].isShowing) {
        // 关闭菜单方法
        [[CK_MenuViewController getMenuViewController] hideLeftViewController];
    }else {
        // 打开菜单方法
        [[CK_MenuViewController getMenuViewController] showLeftViewController];
    }
}

// 点击菜单传递的值
- (void)didClickMenuIndex:(NSInteger)index Title:(NSString *)title {
    
    NSLog(@"第%ld项：%@",(long)index,title);
    
    [self.navigationController popToRootViewControllerAnimated:NO];
    
    switch (index) {
        case 1:
        {
            ZXLTopicController *topicVC = [[ZXLTopicController alloc] init];
            [self.navigationController pushViewController:topicVC animated:NO];
        }
            break;
        case 2:
        {
            ZXLRadioController *radioVC = [[ZXLRadioController alloc] init];
            [self.navigationController pushViewController:radioVC animated:NO];
        }
            break;
        case 3:
        {
            ZXLGoodsController *goodsVC = [[ZXLGoodsController alloc] init];
            [self.navigationController pushViewController:goodsVC animated:NO];
        }
            break;
        default:
            break;
    }

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
